<?php
namespace Modules\Products\Controllers;

class ClassController extends \Phalcon\Mvc\Controller {
	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function indexAction() {
		$currentPage = abs($this -> request -> getQuery('page', 'int', 1));
		if($currentPage <= 0) { $currentPage = 1; }

		$currentLimit = abs($this -> request -> getQuery('limit', 'int', 10));
		if($currentLimit <= 0) { $currentLimit = 10; }

		$service = $this -> productsClass::getServiceInstance() -> pager([
			'order' => 'products_class_id DESC'
		], $currentLimit, $currentPage);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['pager']]);
	}

	public function findFirstAction($products_class_id) {
		$service = $this -> productsClass::getServiceInstance() -> findFirstById($products_class_id);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success','products_class'=>$service['products_class']]);
	}

	public function createAction() {
		$validation = $this -> productsClass::getValidationInstance()
		-> setFields($this -> request -> getPost())
		-> addName()
		-> addStatus()
		-> execute();
		if($validation['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$validation['code'],'msg'=>$validation['message']]);
		}

		$service = $this -> productsClass::getServiceInstance() -> create($validation['fields']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'创建成功']);
	}

	public function updateAction($products_class_id) {
		$validation = $this -> productsClass::getValidationInstance()
		-> setFields($this -> request -> getPut())
		-> addName()
		-> addStatus()
		-> execute();
		if($validation['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$validation['code'],'msg'=>$validation['message']]);
		}

		$service = $this -> productsClass::getServiceInstance() -> update($products_class_id, $validation['fields']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'修改成功']);
	}

	public function deleteAction($products_class_id) {
		$service = $this -> productsClass::getServiceInstance() -> delete($products_class_id);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'删除成功']);
	}
}